package com.zzxz.practicaltrainingsys.controller;

import com.zzxz.practicaltrainingsys.entity.ChatRecord;
import com.zzxz.practicaltrainingsys.service.ChatRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/chat-records")
public class ChatRecordController {

    @Autowired
    private ChatRecordService chatRecordService;

    /**
     * 获取聊天记录
     */
    @GetMapping("/{chatType}/{inviteCode}")
    public Map<String, Object> getChatRecords(
            @PathVariable String chatType,
            @PathVariable String inviteCode,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int size) {

        List<ChatRecord> records = chatRecordService.getChatRecords(chatType, inviteCode, page, size);
        int total = chatRecordService.getChatRecordCount(chatType, inviteCode);

        Map<String, Object> result = new HashMap<>();
        result.put("records", records);
        result.put("total", total);
        result.put("page", page);
        result.put("size", size);

        return result;
    }

    /**
     * 获取用户聊天记录
     */
    @GetMapping("/user/{username}")
    public Map<String, Object> getUserChatRecords(
            @PathVariable String username,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int size) {

        List<ChatRecord> records = chatRecordService.getUserChatRecords(username, page, size);

        Map<String, Object> result = new HashMap<>();
        result.put("records", records);
        result.put("page", page);
        result.put("size", size);

        return result;
    }

    /**
     * 清理旧聊天记录
     */
    @DeleteMapping("/clean/{daysAgo}")
    public Map<String, Object> cleanOldRecords(@PathVariable int daysAgo) {
        int deletedCount = chatRecordService.cleanOldChatRecords(daysAgo);

        Map<String, Object> result = new HashMap<>();
        result.put("deletedCount", deletedCount);
        result.put("message", "成功清理 " + deletedCount + " 条聊天记录");

        return result;
    }
}